class MessageManager {
  constructor() {
    this.localCache = new IndexedDB('chat_messages');
  }

  async handleMessageLoad() {
    // 1. 立即显示缓存的消息
    const cached = await this.localCache.getMessages(conversationId);
    updateUI(cached);

    // 2. 后台同步新消息
    const latestCachedMessage = cached[cached.length - 1];
    const newMessages = await this.fetchNewMessages(
      conversationId, 
      latestCachedMessage?.timestamp
    );

    // 3. 更新UI和缓存
    if (newMessages.length > 0) {
      const merged = this.mergeMessages(cached, newMessages);
      updateUI(merged);
      await this.localCache.updateMessages(merged);
    }
  }
}
